<?php

namespace App\Http\Resources\Stats\User\Player;

use App\Http\Resources\Resource;

class WeekResource extends Resource
{

    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $charts = [
            'timed' => [],
            'data' => [
                'u_game' => [],
                'u_sign' => [],
                'nu_game' => [],
                'nu_sign' => [],
            ],
        ];

        list($timed, $timedEnd) = $this->resource['datetime'];

        $stats = array_column($this->resource['stats']->toArray(), null, 'timed');

        for (; $timed < $timedEnd; $timed += 86400 * 7) {
            $charts['timed'][] = date('W', $timed);
            $charts['data']['u_game'][] = $stats[$timed]['u_game'] ?? null;
            $charts['data']['u_sign'][] = $stats[$timed]['u_sign'] ?? null;
            $charts['data']['nu_game'][] = $stats[$timed]['nu_game'] ?? null;
            $charts['data']['nu_sign'][] = $stats[$timed]['nu_sign'] ?? null;
        }

        return $this->getResponse()->setResponseData($charts);
    }
}
